package com.open.pokergames.biz.views.rooms.fragments

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.jiangyy.easydialog.SingleChoiceDialog
import com.open.pokergames.R
import com.open.pokergames.base.fragment.EventBusBaseFragment
import com.open.pokergames.biz.viewModel.BizViewModel
import com.open.pokergames.biz.views.rooms.RoomAddActivity
import com.open.pokergames.comm.repository.entity.ResultInfo
import com.open.pokergames.texas.repository.entity.RoomEntity
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.room_fragment_add_dezhou.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class DeZhou5Fragment : EventBusBaseFragment(), View.OnClickListener {

    private var viewModel: BizViewModel? = null
    private var currView: View? = null
    private var rooVo: RoomEntity? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.room_fragment_add_dezhou, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = BizViewModel()
        submitBtn.setOnClickListener {
            submit()
        }
        rooVo = RoomEntity()

        gameSizeItem.setOnClickListener(this)
//        partnerItem.setOnClickListener(this)
        roomLeaderPcItem.setOnClickListener(this)
        partnerPcItem.setOnClickListener(this)
    }

    private fun submit() {
        val roomName = roomName.text.toString()
        val roomDes = roomDes.text.toString()
        if (XUtils.string().isEmpty(roomName)) {
            XUtils.toast().show("房间名称不能为空")
            return
        }
        if (XUtils.string().isEmpty(roomDes)) {
            XUtils.toast().show("房间描述不能为空")
            return
        }

        rooVo!!.groupId = RoomAddActivity.groupId
        rooVo!!.roomName = roomName
        rooVo!!.roomDes = roomDes
        rooVo!!.memberSize = 5 // 5人
        rooVo!!.minBet = if (XUtils.string().isEmpty(minBet.text.toString())) 0f else minBet.text.toString().toFloat()
        rooVo!!.maxBet = if (XUtils.string().isEmpty(maxBet.text.toString())) 0f else maxBet.text.toString().toFloat()
        rooVo!!.minCast = if (XUtils.string().isEmpty(minCast.text.toString())) 0f else minCast.text.toString().toFloat()

//        if (XUtils.string().isNotEmpty(rooVo!!.partnerAccount) && rooVo!!.partnerPc == 0) {
//            XUtils.toast().show("已选合伙人，请设置分成比例")
//            return
//        }

//        0：普通；1：5张
        rooVo!!.roomType = 1
        showLoadingDialog("正在创建")
        viewModel!!.roomAdd(rooVo!!)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<RoomEntity>) {

        if ("roomAdd7" != result.eventType) {
            return
        }
        hideLoadingDialog()
        when (result.code) {
            ResultInfo.SUCCESS -> {
                activity?.finish()
            }
            else -> {
                XUtils.toast().showLong(result.msg)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        when (requestCode) {
//            500 -> {
//                if (resultCode == 200 && data != null) {
//                    val user: MemberEntity = data.getSerializableExtra("item") as MemberEntity
//                    partnerName.text = user.userName
//                    rooVo!!.partnerName = user.userName
//                    rooVo!!.partnerAccount = user.account
//                }
//                if (resultCode == 201) {
//                    partnerName.text = ""
//                    rooVo!!.partnerName = ""
//                    rooVo!!.partnerAccount = ""
//                }
//            }
        }

    }

    override fun onClick(v: View?) {
        when (v) {
            gameSizeItem->{
                SingleChoiceDialog
                    .Builder(activity)
                    .setTitle("总局数")
                    .addList(getGameSizeList())
                    .setOnItemClickListener { data, data0 ->
                        gameSize.text = data
                        rooVo!!.gameSize = data.replace("局", "").toInt()
                    }
                    .show()
            }
//            partnerItem -> {
//                XUtils.intent().openActivity(activity, UserSearchSingleSelectedActivity::class.java)
//                    .requestCode(500)
//                    .startInFragment(this)
//            }
            roomLeaderPcItem -> {
                SingleChoiceDialog
                    .Builder(activity)
                    .setTitle("房主分成")
                    .addList(getPcValue())
                    .setOnItemClickListener { data, data0 ->
                        roomLeaderPc.text = data
                        rooVo!!.roomLeaderPc = data.replace("%", "").toInt()
                    }
                    .show()
            }
            partnerPcItem -> {
                SingleChoiceDialog
                    .Builder(activity)
                    .setTitle("合伙分成")
                    .addList(getPcValue())
                    .setOnItemClickListener { data, data0 ->
                        partnerPc.text = data
                        rooVo!!.partnerPc = data.replace("%", "").toInt()
                    }
                    .show()
            }
        }
    }

    private fun getGameSizeList(): ArrayList<String> {
        val list = ArrayList<String>();
        list.add("20局")
        list.add("15局")
        list.add("10局")
        list.add("5局")
        return list;
    }

    private fun getPcValue(): ArrayList<String> {
        val list = ArrayList<String>();
        list.add("100%")
        list.add("90%")
        list.add("80%")
        list.add("70%")
        list.add("60%")
        list.add("50%")
        list.add("40%")
        list.add("30%")
        list.add("20%")
        list.add("10%")
        list.add("0%")
        return list;
    }
}